﻿using Mengsk.ProxyDelivery.Domain;
using Mengsk.ProxyDelivery.Print.Domain.DeliveryNumberGenerator;
using Mengsk.ProxyDelivery.Service;
using Mengsk.ProxyDelivery.Service.Delivery;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Mengsk.ProxyDelivery.Print.UI
{
    /// <summary>
    /// Interaction logic for WuliuNumberUserControl.xaml
    /// </summary>
    public partial class WuliuNumberUserControl : UserControl
    {
        private bool myLoaded = false;

        public WuliuNumberUserControl()
        {
            InitializeComponent();
        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            if (this.myLoaded)
            {
                return;
            }
            this.myLoaded = true;
            var list = DeliveryService.GetDeliveryCompanies().ToList();
            list.Insert(0, "");
            this.cbbDeliveryCompany.ItemsSource = list;
        }

        private void btnSearch_Click(object sender, RoutedEventArgs e)
        {
            string sId = this.tbOrderId.Text.Trim();
            string deliveryNumber = this.tbDeliveryNumber.Text.Trim();
            DateTime startTime = this.dpStart.Value == null ? DateTime.MinValue : this.dpStart.Value.Value;
            DateTime endTime = this.dpEnd.Value == null ? DateTime.MinValue : this.dpEnd.Value.Value;
            if (string.IsNullOrWhiteSpace(sId) && String.IsNullOrWhiteSpace(deliveryNumber) && startTime == DateTime.MinValue)
            {
                MessageBox.Show("查询信息不能全为空");
                return;
            }
            try
            {
                var wuliuNumbers = ServiceContainer.GetService<WuliuNumberService>().GetByAll(sId, this.cbbDeliveryCompany.Text, deliveryNumber, startTime, endTime, 0, 0);
                this.dgvWuliuNumbers.ItemsSource = wuliuNumbers.Datas;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void btnCancelAndDelete_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                if (this.dgvWuliuNumbers.SelectedCells.Count < 1)
                {
                    throw new Exception("未选择任何数据");
                }

                var wuliuNumber = this.dgvWuliuNumbers.SelectedCells[0].Item as WuliuNumber;
                if (wuliuNumber == null)
                {
                    throw new Exception("选择的数据对象不是 WuliuNumber");
                }

                wuliuNumber = ServiceContainer.GetService<WuliuNumberService>().GetById(wuliuNumber.Id);
                if (wuliuNumber == null)
                {
                    throw new Exception("该信息已经被解绑并删除");
                }

                string msg = string.Format("是否解绑并删除:{0},{1}", wuliuNumber.WuliuId, wuliuNumber.DeliveryNumber);
                if (MessageBox.Show(msg, "警告", MessageBoxButton.YesNo, MessageBoxImage.Asterisk) != MessageBoxResult.Yes)
                {
                    return;
                }
                DeliveryNumberGeneratorManager.CancelOnline(wuliuNumber);
                //ServiceContainer.GetService<WuliuNumberService>().Delete(wuliuNumber);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
    }
}
